﻿'use strict';

// todoPage controller
app.controller('productMix', ['$state','$filter','$window','$http','$scope','$modal','toaster','$log','$timeout', function($state,$filter,$window,$http,$scope,$modal,toaster,$log,$timeout) {
    $scope.goods=[];//定义数组
    //删除一行
    $scope.dele =function($index){
        $scope.goods.splice($index,1);
    };
    //添加的方法
    $scope.addRows=function(){
        var id = new Date().getTime();
        var go = {
            "flag":false,
            "productMixDetailsId":id,
            "productMixDetailsHpbm":"",
            "productMixDetailsHpmc":"",
            "productMixDetailsGgxh":"",
            "productMixDetailsHpdw":"",
            "productMixDetailsHpsl":"",
            "productMixDetailsHpdj":"",
            "productMixDetailsHpzje":"",
            "productMixDetailsBz":""
        }
        $scope.goods.push(go);
    };
    $scope.SelectPrice = function(index,bm){
        if(bm.productMixDetailsHpbm && bm.productMixDetailsHpbm!==""){
            toaster.getInfo('/a_yongan/list/goodsprice/'+bm.productMixDetailsHpbm,null).then(function(data){
                if(data && data.data && data.data.data){
                    console.log(data.data.data);
                    var modalInstance = $modal.open({
                        templateUrl: 'selectPrice.html',
                        controller: 'selectPriceCtrl',
                        size: 'sm',
                        resolve: {
                            items: function () {
                                return data.data.data;
                            }
                        }
                    });
                    modalInstance.result.then(function (selectedItem) {
                        $scope.goods[index].productMixDetailsHpdj = selectedItem;
                        $scope.goods[index].productMixDetailsHpzje = parseFloat(selectedItem)*parseFloat($scope.goods[index].productMixDetailsHpzje);
                        showMessage('success','','选择成功！');
                    }, function () {
                        $log.info('Modal dismissed at: ' + new Date());
                    });
                }
            });
        }else{
            showMessage('warning','','货品编码不能为空！');
        }
    };
    function objToGo(obj,index){
        $scope.goods[index].productMixDetailsHpbm=obj.id;
        $scope.goods[index].productMixDetailsHpmc=obj.name;
        $scope.goods[index].productMixDetailsGgxh=obj.hpxh;
        $scope.goods[index].productMixDetailsHpdw=obj.dw;
        $scope.goods[index].productMixDetailsHpdj=obj.dj;
        $scope.goods[index].productMixDetailsHpzje=parseInt($("#"+$scope.goods[index].productMixDetailsId+"productMixDetailsHpsl").val())*parseFloat(obj.dj);
    }
    $scope.detailsInput = {
        sl:function(index){
            var sl = $("#"+$scope.goods[index].productMixDetailsId+"productMixDetailsHpsl").val();
            var dj = $scope.goods[index].productMixDetailsHpdj;
            $scope.goods[index].productMixDetailsHpsl = sl;
            if(dj===""){
                showMessage('warning','',"货品信息不能为空！");
                return false;
            }
            $scope.goods[index].productMixDetailsHpzje = parseFloat(dj)*parseInt(sl);
        },
        bz:function(index){
            $scope.goods[index].productMixDetailsBz = $("#"+$scope.goods[index].productMixDetailsId+"productMixDetailsBz").val();
        }
    };
    function addDo(){
        // $scope.goods.forEach(function(item){
        //     item.salesOrderDetailsDhrq=$("#"+item.salesOrderDetailsId+"salesOrderDetailsDhrq").val();
        // });
        var notesdata = {};
        if(!$scope.row.productStructureListId){
            notesdata = {
                "productStructureListId":"",
                "qy":$("#qy").prop("checked"),
                "zdr":$scope.app.userName,
                "zdrId":$scope.app.userId,
                "productMixHpbm":$("#productMixHpbm").val(),
                "productMixSpmc":$("#productMixSpmc").val(),
                "productMixGg":$("#productMixGg").val(),
                "productMixDw":$("#productMixDw").val(),
                "productMixType":$("#productMixType").val(),
                "productStructureListBz":$("#productStructureListBz").val(),
                "details":$scope.goods
            }
        }else{
            notesdata = {
                "productStructureListId":$scope.row.productStructureListId,
                "qy":$("#qy").prop("checked"),
                "productMixHpbm":$("#productMixHpbm").val(),
                "productMixSpmc":$("#productMixSpmc").val(),
                "productMixGg":$("#productMixGg").val(),
                "productMixDw":$("#productMixDw").val(),
                "productMixType":$("#productMixType").val(),
                "productStructureListBz":$("#productStructureListBz").val(),
                "details":$scope.goods
            }
        }
        var info="";
        toaster.postInfo('/a_yongan/add/productstructurelist',notesdata).then(function(data){
            if(data && data.data && data.data.success){
                showMessage('seccess','','保存成功！');
                $scope.warehouseAdd = !($scope.warehouseAdd);
                $scope.warehouseList = !($scope.warehouseList);
                getList();
                return info;
            }else{
                showMessage('warning','',"保存失败！");
                info = "请求错误！";
            }
        });
        return info;
    }
    //改变每行chekbox的状态
    $scope.ck = function($index){
        $scope.goods[$index].flag=!$scope.goods[$index].flag
    }
    //批量删除
    $scope.plsc = function(){
    //反着遍历
        for (var i = $scope.goods.length-1;i>=0;i--) {
            if ($scope.goods[i].flag) {
                $scope.goods.splice(i,1);
            }
        }
    }
    //全选
    var qq = true;
    $scope.qx = function(){
    //获取属性
        var ck = $("input[name=ck]")
        for (var i=0;i<ck.length;i++) {
            ck[i].checked=qq;
            //给每个数组中的ck赋值
            $scope.goods[i].flag=qq;
        }
        qq=!qq;
    };

    $scope.title = {
        title:'产品结构清单列表',
        addTitle:'产品结构清单信息'
    };
    $scope.toaster = {
        type: 'warning',
        title: '提示：',
        text: '内容不能为空！'
    };
    function makeData(data){
        data.forEach(function (item) {
            item.createTime = $filter("date")(item.createTime, "yyyy/MM/dd");
            if(item.qy=='true'){
                item.qy = true;
                item.status = '使用中...';
                item.status1 = 'label bg-info';
            }else{
                item.qy = false;
                item.status = '禁用中...';
                item.status1 = 'label bg-danger';
            }
        })
        $scope.saveDate = data;
    }
    function getList(){
        toaster.getInfo('/a_yongan/list/productstructurelist',null).then(function(data){
            if(data && data.data && data.data.data){
                makeData(data.data.data);
            }
        });
    }
    getList();
    $scope.pop = function(){
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    };
    function showMessage(type,title,text){
        $scope.toaster.type = type;
        $scope.toaster.text = text;
        toaster.pop($scope.toaster.type, $scope.toaster.title, $scope.toaster.text);
    }
    function checkItems(){
        var items = ['productMixHpbm'];
        var itemsValue = ['商品编码'];
        for(var i=0;i<items.length;i++){
            if($('#'+items[i]+'').val()===''){
                $('#'+items[i]+'').focus();
                showMessage('warning','',itemsValue[i]+"不能为空！");
                return false;
            }
        }
        return true;
    }
    $scope.message = '是否确定保存？';
    $scope.saveInfo = function (size) {
        if(!checkItems()){
            return;
        }
        var modalInstance = $modal.open({
            templateUrl: 'myModalContent.html',
            controller: 'ModalInstanceCtrl',
            size: size,
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            var info = addDo();

        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };
    $scope.isshow=true;
    $scope.warehouseAdd = false;
    $scope.warehouseList = true;
    $scope.warehouseJump = function(ts) {
        if(ts!=='back'){
            $scope.row = {};
            $scope.goods=[];
            $scope.row = {
                'createTime':$filter("date")(new Date(), "yyyy/MM/dd"),
                'zdr':$scope.app.userName,
                'qy':true
            };
        }else{
            getList();
        }
        $scope.warehouseAdd = !($scope.warehouseAdd);
        $scope.warehouseList = !($scope.warehouseList);
    };
    $scope.row = {};
    $scope.columns = [
        {
            title:'商品编码',
            show:true
        },
        {
            title:'商品名称',
            show:true
        },
        {
            title:'型号规格',
            show:true
        },
        {
            title:'计量单位',
            show:true
        },
        {
            title:'制单人',
            show:true
        },
        {
            title:'创建时间',
            show:true
        },
        {
            title:'清单状态',
            show:true
        }
    ];
    $scope.saveDate=[];
    $scope.showInfo = function(row){
        $scope.warehouseAdd = !($scope.warehouseAdd);
        $scope.warehouseList = !($scope.warehouseList);
        $scope.row = row;
        $scope.goods = row.details;
    };
    $scope.showSelectGoods = function(obj,index){
        var modalInstance = $modal.open({
            templateUrl: 'selectGoods.html',
            controller: 'selectGoodsCtrl',
            size: 'sm',
            resolve: {
                items: function () {
                    return $scope.message;
                }
            }
        });
        modalInstance.result.then(function (selectedItem) {
            if(obj==null){
                $scope.row.productMixHpbm=selectedItem.id;
                $scope.row.productMixSpmc=selectedItem.name;
                $scope.row.productMixGg=selectedItem.hpxh;
                $scope.row.productMixDw=selectedItem.dw;
                $scope.row.productMixType=selectedItem.type;
            }else{
                objToGo(selectedItem,index);
            }
            showMessage('success','','选择成功！');
            //$scope.selected = selectedItem;
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };

}])
;